package com.ynet.middleground.approve.mq.producer;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.approve.mq.Source;

/**
 * 测试 MQ 专用
 * 
 * @author liulx
 */
@EnableBinding({Source.class})
@Component
public class SourceProducer {

    @Autowired
    private Source source;

    /**
     * 测试专用，往 MQ 推送消息
     * 
     * @param msg
     *            推送消息内容
     */
    public void sendMessages(String msg) {
        IfpLogger.info("mq", "MQ 测试，往Binding-{}推送信息-{}", Source.O_CONTRACT, msg);
        String payload = msg;
        Map<String, Object> headers = new HashMap<>(1);
        // tags 标签对应合约中心 yml 配置文件
        headers.put("TAGS", "tag_test");
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<String> message = MessageBuilder.createMessage(payload, messageHeaders);
        this.source.noticeContract().send(message);
    }

}
